//
//  APPLEComposeToolBar.swift
//  123
//
//  Created by pangjiaxing on 16/1/9.
//  Copyright © 2016年 pangjiaxing. All rights reserved.
//

import UIKit

enum APPLEComposeToolBarButtonType: Int {
    
    case  Picture = 1000
    case  Mention = 1001
    case  Trend = 1002
    case  Emoticon = 1003
    case  Add = 1004
    
}


class APPLEComposeToolBar: UIStackView {
    
    var buttonClickCloures:((type:APPLEComposeToolBarButtonType)->())?
    
    var  isSystemKeyboard : Bool = true {
        didSet{
            let button = viewWithTag(APPLEComposeToolBarButtonType.Emoticon.rawValue) as! UIButton
            var imageName = "compose_keyboardbutton_background"
            if isSystemKeyboard {
                imageName = "compose_emoticonbutton_background"
            }
            button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
            button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        }
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI(){
        
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
    }
    
    private func addChildButton(imageName:String,type:APPLEComposeToolBarButtonType){
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        button.backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        button.tag=type.rawValue
        button.addTarget(self, action: "didClickToolBarButton:", forControlEvents: UIControlEvents.TouchUpInside)
        addArrangedSubview(button)
    }
    
    @objc private func didClickToolBarButton(button:UIButton) {
        
        
        print("我在APPLEComposeToolBar中")
        buttonClickCloures?(type : APPLEComposeToolBarButtonType(rawValue: button.tag)!)
        
    }
}
